package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specificationOption.TbSpecificationOption;
import com.dongdongshop.service.specificationOption.SpecificationOptionService;
import com.dongdongshop.service.specification.SpecificationService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;

/**
 * 规格
 * @Author Eayon
 * @Date 2020/2/21 18:37
 */
@Controller
@RequestMapping("specification")
public class SpecificationController {

    @Reference
    private SpecificationService specificationService;

    @Reference
    private SpecificationOptionService specificationOptionService;

    private Logger logger = LoggerFactory.getLogger(SpecificationController.class);
    

    /**
     * 跳转规格管理页
     */
    @RequestMapping("toSpecificationList")
    public String toSpecificationList(){
        return "specification/specification";
    }

    /**
     * 查询所有规格信息 不分页
     */
    @RequestMapping(value = "getSpecificationInfo",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getSpecificationInfo(){
        List<TbSpecification> list = null;
        try {
            list = specificationService.getSpecificationInfo();
        }catch (Exception e){
            logger.error("查询所有规格信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),list);
    }

    /**
     * 跳转增加规格页
     */
    @RequestMapping("toAddSpecificationPage")
    public String toAddSpecificationPage(){
        return "specification/add";
    }

    /**
     * 添加规格 和 规格选项数组
     */
    @RequestMapping("addSpecificationAndOption")
    @ResponseBody
    public DataResult addSpecificationAndOption(TbSpecification s, @RequestParam("result[]")String[] result){
        try {
            List<TbSpecificationOption> specificationOptions = new ArrayList<>();
            //获取前台传来的规格选项json字符串数据 并进行遍历
            for (String str : result) {
                //将每个规格选项json字符串转换成规格选项对象  并存入集合
                TbSpecificationOption so = JSONObject.parseObject(str,TbSpecificationOption.class);
                specificationOptions.add(so);
            }
            //添加规格 和 规格选项
            specificationService.addSpecificationAndOption(s,specificationOptions);
        }catch (Exception e){
            logger.error("添加规格 和 规格选项异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 跳转规格修改页，需要查询该规格信息和该规格相关的规格选项信息
     * @param id
     * @return
     */
    @RequestMapping("toUpdateSpecificationPage")
    public String toUpdateSpecificationPage(Long id, Model model){
        try {
            //根据规格id查询规格信息
            TbSpecification s = specificationService.getSpecificationById(id);
            model.addAttribute("s",s);
            //根据规格id查询该规格拥有的规格选项
            List<TbSpecificationOption> so = specificationOptionService.getspecificationOptionsBySpecificationId(id);
            model.addAttribute("so",so);
        } catch (Exception e){
            logger.error("跳转规格修改页异常",e);
        }
        return "specification/update";
    }

    /**
     * 修改规格信息
     * @param s
     * @param result
     * @return
     */
    @RequestMapping(value = "updateSpecification",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateSpecification(TbSpecification s,@RequestParam("result[]")String[] result){
        try {
            List<TbSpecificationOption> specificationOptions = new ArrayList<>();
            //获取前台传来的规格选项json字符串数据 并进行遍历
            for (String str : result) {
                //将每个规格选项json字符串转换成规格选项对象  并存入集合
                TbSpecificationOption so = JSONObject.parseObject(str,TbSpecificationOption.class);
                specificationOptions.add(so);
            }
            //修改规格 和 规格选项
            specificationService.updateSpecification(s,specificationOptions);
        }catch (Exception e){
            logger.error("修改规格信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }


    /**
     * 根据规格id删除规格信息以及对应的规格选项信息
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteSpecificationById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult deleteSpecificationById(Long id){
        try {
            specificationService.deleteSpecificationById(id);
        }catch (Exception e){
            logger.error("根据规格id删除规格信息以及对应的规格选项信息方法异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
}
